<?php

namespace S\Validate;

/**
 * 参数验证类:使用举例
 *
 * user => ['value' => '', rule => 'filter', required => false, option => []];
 *
 */
class Handler {
    const FILTER    = 1,
        VALIDATOR   = 2;

    private static $_default_validator = array(
        'date'     =>  'Type\\Date',
        'digit'    =>  'Type\\Digit',
        'email'    =>  'Type\\Email',
        'in'       =>  'Type\\In',
        'ip'       =>  'Type\\Ip',
        'phone'    =>  'Type\\Phone',
        'regx'     =>  'Type\\Regx',
        'str'      =>  'Type\\Str',
        'url'      =>  'Type\\Url',
        'money'    =>  'Type\\Money',
        'card'     =>  'Type\\Card',
        'identify' =>  'Type\\Identify',
        'file'     =>  'Type\\File',
    );

    /**
     * 参数检查入口
     * 根据rule确定具体的验证方法，并将value，filter，option作为参数调用该调用
     *
     * @param array $params
     * @return null
     */
    public static function check(array $params) {
        $ret = array();
        foreach ($params as $key=>$value) {
            //非必要参数,但是如果存在的话一定进行验证
            if (isset($value['required']) && $value['required'] === false) {
                if (!$value['value']) {
                    continue;
                }
            }

            if (!is_array($value)) {
                $rule = $key;
                $param = array($value, self::VALIDATOR);
            } else {
                if (isset($value['rule'])) {
                    if ($value['rule'] === 'filter') {
                        $ret[$key] = filter_var($value['value'], FILTER_SANITIZE_FULL_SPECIAL_CHARS);
                        continue;
                    } elseif ($value['rule'] === 'none') {
                        $ret[$key] = $value['value'];
                        continue;
                    }
                    $rule = $value['rule'];
                } else {
                    $rule = $key;
                }

                $param = array($value['value']);
                array_push($param, isset($value['filter']) ? $value['filter'] : self::VALIDATOR);
                array_push($param, isset($value['option']) ? $value['option'] : array());
            }

            if (is_string($rule) && isset(self::$_default_validator[$rule])) {
                $class = '\\'.__NAMESPACE__ .'\\'. self::$_default_validator[$rule];
                $tmp_class  = new $class;
                $tmp_ret    = call_user_func_array($tmp_class, $param);
                /* @var $tmp_class Abstraction */
                if (is_array($tmp_ret) && $tmp_class->is_extract_array()) {
                    // 注意，这里不会覆盖已存在的key，可能会引起问题，需要注意！
                    $ret += $tmp_ret;
                    continue;
                }
            } else {
                if ($rule instanceof Abstraction) {
                    // 自定义验证类
                    $tmp_ret = call_user_func($rule, $value['value'], isset($value['filter']) ? $value['filter'] : self::VALIDATOR, isset($value['option']) ? $value['option'] : array());
                } else {
                    // 自定义验证函数,此时只接受验证的值参数
                    $tmp_ret = call_user_func($rule, $value['value']);
                }
            }

            $ret[$key] = $tmp_ret;
        }
        return $ret;
    }
}
